<template>
  <el-dialog :title="dialog.title" :visible.sync="dialog.show" width="600px" custom-class="mydialog" append-to-body @close="handleClose">
    <div class="dialog-content">
      <el-form ref="form1" :model="form" :rules="rules" label-width="150px">
        <el-form-item label="发票编码：" >
          <el-input v-model="form.code" maxlength="50" />
        </el-form-item>
        <el-form-item label="发票金额：" >
          <el-input v-model="form.money" maxlength="50" />
        </el-form-item>
        <el-form-item label="开票日期：" >
          <div class="block">
            <el-date-picker
              v-model="value2"
              :picker-options="pickerOptions"
              align="right"
              type="date"
              placeholder="选择日期"/>
          </div>
        </el-form-item>
      </el-form>
      <div class="text-center mt-30">
        <el-button class="greenbt" type="primary" @click="submitBut()">保存</el-button>
      </div>
    </div>
  </el-dialog>
</template>
<script>
import { purchaseInvoice } from '@/api/pas/purchase'
export default{
  props: {
    modalStaticData: {
      type: Object,
      default: () => {}
    },
    // 父组件调用时传进来的参数
    modalData: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      dialog: {
        title: '', show: true
      },
      form: {
        code: '', money: '', day: ''
      },
      rules: {},
      options: [],
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() > Date.now()
        },
        shortcuts: [{
          text: '今天',
          onClick(picker) {
            picker.$emit('pick', new Date())
          }
        }, {
          text: '昨天',
          onClick(picker) {
            const date = new Date()
            date.setTime(date.getTime() - 3600 * 1000 * 24)
            picker.$emit('pick', date)
          }
        }, {
          text: '一周前',
          onClick(picker) {
            const date = new Date()
            date.setTime(date.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', date)
          }
        }]
      },
      value1: '',
      value2: ''
    }
  },
  mounted() {
    this.form.code = this.modalData.invoiceCode
    this.form.money = this.modalData.invoiceMoney
    this.value2 = this.modalData.invoiceDay
  },
  methods: {
    handleClose() {
      this.$emit('close')
    },
    // 保存
    submitBut() {
      if (this.form.code === null || this.form.code === '' || this.form.code === undefined || this.form.money === null || this.form.money === '' || this.form.money === undefined) {
        this.$message({ message: '编码、金额不能为空！' })
        return
      }
      if (isNaN(this.form.money)) {
        this.$message('金额必须是数字！')
        return
      }

      var data = { 'purchaseId': this.modalData.purchaseId, 'invoiceCode': this.form.code, 'invoiceMoney': this.form.money, 'invoiceDay': this.dateToString(this.value2) }
      purchaseInvoice(data)
        .then(response => {
          if (response.data.status === 'success') {
            this.$message({
              message: '成功!'
            })
            this.handleClose()
          } else {
            this.options = []
          }
        }).finally(() => {
          this.modalStaticData.closeChildAndRefresh()
        })
    },
    dateToString(date) {
      var year = date.getFullYear()
      var month = (date.getMonth() + 1).toString()
      var day = (date.getDate()).toString()
      if (month.length === 1) {
        month = '0' + month
      }
      if (day.length === 1) {
        day = '0' + day
      }
      var dateTime = year + '-' + month + '-' + day
      return dateTime
    }

  }
}
</script>

